package cn.fufeiii.bt.houseloan;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class LoanCalculator {

    public static void main(String[] args) {
        String fileAddr = FileUtil.getUserHomePath() + "\\Desktop\\payment-plan-" /*+ System.currentTimeMillis() / 1000*/ + ".xlsx";

        double totalLoanAmount = 1000000; // 贷款金额（单位：元）
        double commercialLoanAmount = 503000; // 商业贷款金额（单位：元）
        double commercialRate = 0.04; // 商业贷款利率
        double accumulationRate = 0.031; // 公积金贷款利率
        int loanTerm = 360; // 贷款期限（单位：月）

        // 计算公积金贷款金额
        double accumulationLoanAmount = totalLoanAmount - commercialLoanAmount;

        // 计算商业贷款每月还款额
        double commercialMonthlyRate = commercialRate / 12; // 商业贷款月利率
        double commercialMonthlyPayment = commercialLoanAmount * commercialMonthlyRate * Math.pow(1 + commercialMonthlyRate, loanTerm) / (Math.pow(1 + commercialMonthlyRate, loanTerm) - 1);

        // 计算公积金贷款每月还款额
        double accumulationMonthlyRate = accumulationRate / 12; // 公积金贷款月利率
        double accumulationMonthlyPayment = accumulationLoanAmount * accumulationMonthlyRate * Math.pow(1 + accumulationMonthlyRate, loanTerm) / (Math.pow(1 + accumulationMonthlyRate, loanTerm) - 1);

        // 计算总的还款信息
        double totalMonthlyPayment = commercialMonthlyPayment + accumulationMonthlyPayment; // 总月还款额
        double totalPayment = totalMonthlyPayment * loanTerm; // 总还款金额
        double totalInterest = totalPayment - totalLoanAmount; // 总利息

        // 输出总的还款信息
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("总贷款金额：" + df.format(totalLoanAmount) + "元");
        System.out.println("总还款金额：" + df.format(totalPayment) + "元");
        System.out.println("总利息：" + df.format(totalInterest) + "元");
        System.out.println("商业贷款金额：" + df.format(commercialLoanAmount) + "元");
        System.out.println("商业贷款利率：" + commercialRate * 100 + "%");
        System.out.println("商业贷款每月还款额：" + df.format(commercialMonthlyPayment) + "元");
        System.out.println("公积金贷款金额：" + df.format(accumulationLoanAmount) + "元");
        System.out.println("公积金贷款利率：" + accumulationRate * 100 + "%");
        System.out.println("公积金贷款每月还款额：" + df.format(accumulationMonthlyPayment) + "元");

        // 输出还款计划
        System.out.println("\n还款计划：");
        System.out.println("期数\t还款金额\t当期总本金\t当期总利息\t商业贷款本金\t商业贷款利息\t公积金贷款本金\t公积金贷款利息");
        double remainingCommercialLoanAmount = commercialLoanAmount;
        double remainingAccumulationLoanAmount = accumulationLoanAmount;
        List<List<Double>> rowList = new ArrayList<>();
        for (int i = 1; i <= loanTerm; i++) {
            double commercialInterest = remainingCommercialLoanAmount * commercialMonthlyRate; // 商业贷款当期利息
            double commercialPrincipal = commercialMonthlyPayment - commercialInterest; // 商业贷款当期本金
            remainingCommercialLoanAmount -= commercialPrincipal; // 商业贷款剩余本金

            double accumulationInterest = remainingAccumulationLoanAmount * accumulationMonthlyRate; // 公积金贷款当期利息
            double accumulationPrincipal = accumulationMonthlyPayment - accumulationInterest; // 公积金贷款当期本金
            remainingAccumulationLoanAmount -= accumulationPrincipal; // 公积金贷款剩余本金

            double monthlyPayment = commercialMonthlyPayment + accumulationMonthlyPayment; // 当期还款金额

            System.out.println(i + "\t" + df.format(monthlyPayment) + "\t" + df.format(commercialPrincipal + accumulationPrincipal) + "\t" + df.format(commercialInterest + accumulationInterest) + "\t" + df.format(commercialPrincipal) + "\t" + df.format(commercialInterest) +
                    "\t" + df.format(accumulationPrincipal) + "\t" + df.format(accumulationInterest));
            rowList.add(List.of(Double.valueOf(String.valueOf(i)), Double.valueOf(df.format(monthlyPayment)), Double.valueOf(df.format(commercialPrincipal + accumulationPrincipal)), Double.valueOf(df.format(commercialInterest + accumulationInterest)),
                    Double.valueOf(df.format(commercialPrincipal)), Double.valueOf(df.format(commercialInterest)), Double.valueOf(df.format(accumulationPrincipal)), Double.valueOf(df.format(accumulationInterest))));
        }

        printToXlsx(fileAddr, rowList);
    }

    private static void printToXlsx(String fileAddr, List<List<Double>> rowList) {
        EasyExcel.write(fileAddr)
                .sheet()
                .head(List.of(List.of("期数"), List.of("还款金额"), List.of("当期总本金"), List.of("当期总利息"), List.of("商业贷款本金"), List.of("商业贷款利息"), List.of("公积金贷款本金"), List.of("公积金贷款利息")))
                .doWrite(rowList)
        ;
    }

}